import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/11/5 21:19
 */

public class Test {
    public static void main(String[] args) {
        //1.通过 Callable 来描述一个任务
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
               int sum = 0;
                for (int i = 1; i <=1000; i++) {
                    sum+=i;
                }
                return sum;
            }
        };
        //2.创建线程，完成计算
        //为了让线程执行 Callable 需要一个辅助类
        FutureTask<Integer> task = new FutureTask<>(callable);
        Thread t =new Thread(task);
        t.start();
        try {
            System.out.println(task.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}
